package com.example.firstdemo.network.api.addProblem

import com.example.firstdemo.network.api.addProblem.data.PositionDictData
import com.example.firstdemo.network.api.addProblem.data.ProblemDictData
import com.example.firstdemo.network.api.addProblem.data.ProblemItem
import com.example.firstdemo.network.api.addProblem.data.VehicleData
import com.example.firstdemo.network.api.system.SystemApi
import com.example.firstdemo.network.service.BaseResponse
import com.example.firstdemo.network.service.RetrofitService
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.MultipartBody
import okhttp3.RequestBody.Companion.asRequestBody
import retrofit2.http.GET
import retrofit2.http.Multipart
import retrofit2.http.POST
import retrofit2.http.Part
import retrofit2.http.Query
import java.io.File

interface AddProblemInterface {
    @GET("vehicle/carriage/problem/realByInspection")
    suspend fun getList(
    ): BaseResponse<List<ProblemItem>>

    @GET("vehicle/carriage/problem/getAutoProblemInfo")
    suspend fun getDetail(
        @Query("passTime")
        passTime: String,
        @Query("uid")
        uid: String
    ): BaseResponse<ProblemItem>

    @GET("vehicle/vehicle/getVehiclesByNo")
    suspend fun getCarriage(
    ): BaseResponse<List<VehicleData>>

    suspend fun getPosition(): BaseResponse<List<PositionDictData>> {
        return SystemApi.getDict("image_position")
    }

    suspend fun getProblem(): BaseResponse<List<ProblemDictData>> {
        return SystemApi.getDict("auto_error")
    }

    @Multipart
    @POST("vehicle/carriage/problem/addByInspection")
    suspend fun addProblem(
        @Query("stationId") stationId: String,
        @Query("passTime") passTime: String,
        @Query("carriageOrder") carriageOrder: String,
        @Query("carriageUid") carriageUid: String,
        @Query("vehicleUid") vehicleUid: String,
        @Query("carriageId") carriageId: Int,
        @Query("indexId") indexId: Int,
        @Query("vehicleId") vehicleId: Int,
        @Query("vehicleNumber") vehicleNumber: String,
        @Query("carriageNo") carriageNo: String,
        @Query("position") position: String,
        @Query("remarks") remarks: String,
        @Query("remarksCode") remarksCode: Int,
        @Query("confirmSuggestion") confirmSuggestion: String,
        @Part filePart: MultipartBody.Part
    ): BaseResponse<Int>

    suspend fun addProblem(
        stationId: String,
        passTime: String,
        carriageOrder: String,
        carriageUid: String,
        vehicleUid: String,
        carriageId: Int,
        indexId: Int,
        vehicleId: Int,
        vehicleNumber: String,
        carriageNo: String,
        position: String,
        remarks: String,
        remarksCode: Int,
        confirmSuggestion: String,

        filePath: String,
        filename: String
    ): BaseResponse<Int> {
        val file = File(filePath)
        val fileBody = file.asRequestBody("image/*".toMediaTypeOrNull())
        val filePart = MultipartBody.Part.createFormData(filename, file.name, fileBody)
        return addProblem(
            stationId, passTime, carriageOrder, carriageUid, vehicleUid,
            carriageId, indexId, vehicleId, vehicleNumber, carriageNo,
            position, remarks, remarksCode, confirmSuggestion, filePart
        )
//        val service = RetrofitClient.retrofit.create(ImageUploadService::class.java)
//        val call = service.uploadImage(descriptionBody, filePart)
//
//        call.enqueue(object : Callback<ResponseBody> {
//            override fun onResponse(call: Call<ResponseBody>, response: Response<ResponseBody>) {
//                if (response.isSuccessful) {
//                    // 处理成功
//                } else {
//                    // 处理失败
//                }
//            }
//
//            override fun onFailure(call: Call<ResponseBody>, t: Throwable) {
//                // 处理失败
//            }
//        })
    }
}

object AddProblemApi : AddProblemInterface by RetrofitService.create()